﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using GPI.Server;
using Model;
using System.Text;
using Moon.Common.WebInfo;
using Moon.Common;
using System.IO;
using Server;

public partial class Admin_ContentManage_SinglePageByDefault : System.Web.UI.Page
{
    NodeService server = new NodeService();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["id"] != null)
            {
               
                //获得查询的ID号
                string id = Request.QueryString["id"].ToString();
                this.lbnEditNode.CommandArgument = id;
                this.lbnEditContent.CommandArgument = id;
                NodeInfo model = server.GetModel(id.ToInt(0));
                this.labTitle.Text = model.NodeName;
                //绑定列表
                this.repNodeList.DataSource = server.GetAllListByCh(id);
                this.repNodeList.DataBind();
                //绑定内容编辑页面
                this.txtContent.Text = model.Content;
                this.hfImage.Value = model.NodeImage;
                this.lbnDefaultAdd.CommandArgument = id;
            }
            else
            {
                //this.form1.Visible = false;
                this.form1.InnerText = "404 Not Found!";
            }
        }
    }

    protected void lbnEditNode_Click(object sender, EventArgs e)
    {
        string id = this.lbnEditNode.CommandArgument;
        //绑定列表
        this.repNodeList.DataSource = server.GetAllListByCh(id);
        this.repNodeList.DataBind();
        this.MultiView1.ActiveViewIndex = 0;
    }
    //编辑按钮
    protected void lbnEditContent_Click(object sender, EventArgs e)
    {
        this.MultiView1.ActiveViewIndex = 1;
        if (this.txtContent.Text.IsNullOrEmpty())
        {
            string id = this.lbnEditContent.CommandArgument;
            NodeInfo model = server.GetModel(id.ToInt(0));
            //绑定内容编辑页面
            this.txtContent.Text = model.Content;
        }
    }
    //返回更新用的Url
    public string getUrl(object tmp, object id)
    {
        ListItem obj = SystemConfigInfo.TypeMap.FindByText(tmp.ToString());

        return obj != null ? obj.Value + id :"";
    }
    //保存模板
    protected void btnSaveTemplate_Click(object sender, EventArgs e)
    {
        //获得内容编辑的ID号
        string id = this.lbnEditContent.CommandArgument;
        //获得编辑内容
        NodeInfo model = server.GetModel(id.ToInt(0));
        if (!model.IsHTML)
        {
            model.Url = WebConfigInfo.ShowAction + model.Id;
            model.Content = this.txtContent.Text;
            if (this.fuImage.HasFile)
                model.NodeImage = WebConfigInfo.ImgPath + Guid.NewGuid().ToString() + this.fuImage.FileName.GetFileExt();
            else
                model.NodeImage = this.hfImage.Value;
            if (uPFile(this.fuImage,model.NodeImage.GetFileName(),Application["WebPath"]+WebConfigInfo.ImgPath,WebConfigInfo.Imgformat)&&server.Update(model))
                MessageBox.Show(this.Page, "操作成功！");
            else
                MessageBox.Show(this.Page, "操作失敗！");
        }
        else
        { }
        this.MultiView1.ActiveViewIndex = 0;
        //绑定列表
        this.repNodeList.DataSource = server.GetAllListByCh(id);
        this.repNodeList.DataBind();
    }


    /// <summary>
    /// 上傳操作
    /// </summary>
    /// <param name="Fupload">上傳空間</param>
    /// <param name="SaveName">保存名</param>
    /// <param name="dir">保存目錄</param>
    /// <param name="CheckType">驗證類型字符串</param>
    /// <returns></returns>
    private bool uPFile(System.Web.UI.WebControls.FileUpload Fupload, string SaveName, string dir, string CheckType)
    {

        /*如果上傳控件存在文件*/
        if (!this.hfImage.Value.IsNullOrEmpty())
        {
            CoFileHepler.DeleteFile(Application["WebPath"] + this.hfImage.Value);
        }
            
            /*
             * 判斷目錄是否存在不存在則進行創建
             */
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            try
            {
                //獲得文件類型
                string type = CoFileHepler.GetPostfixStr(Fupload.FileName);
                //判斷文件類型是否為需要的類型
                if (CoFileHepler.CheckValidExt(type, CheckType))
                {
                    Fupload.PostedFile.SaveAs(dir + SaveName);
                }
                return true;
            }
            catch
            {
                return false;
            }
    }


    #region 首页展示模块
    /// <summary>
    ///绑定信息列表
    /// </summary>
    /// <param name="type"></param>
    /// <param name="id"></param>

    private void BindTable(string id)
    {
        DictionaryService server = new DictionaryService();
        DataSet ds = null;
        ds = server.GetList(" Nodeid=" + id);
        if (ds != null)
        {
            this.repDictionaryList.DataSource = ds;
            this.repDictionaryList.DataBind();
        }
        this.MultiView1.ActiveViewIndex = 2;
    }
    /// <summary>
    /// 信息表动作
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void repDictionaryList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        DictionaryService server = new DictionaryService();
        int id = 0;
        DictionaryInfo model = new DictionaryInfo();
        switch (e.CommandName)
        {

            case "edit":
                this.MultiView1.ActiveViewIndex = 3;
                this.btnSaveClass.CommandName = "";
                id = e.CommandArgument.ObjectToInt(0);

                model = server.GetModel(id);
                this.txtClassName.Text = model.Name;
                this.txtValues.Text = model.Values;
                this.btnSaveClass.CommandArgument = model.id.ToString();
                this.ddlType.SelectedValue = model.Type;
                this.labImagePath.Text = model.ImagePath;
                seleteTag();
                break;
            case "delete":
                id = e.CommandArgument.ObjectToInt(0);
                model = server.GetModel(id);
                if (server.Delete(id))
                {
                    MessageBox.Show(this.Page, "操作成功！");
                }
                else
                {
                    MessageBox.Show(this.Page, "操作失敗！");
                }
                BindTable(this.lbnDefaultAdd.CommandArgument);
                this.MultiView1.ActiveViewIndex = 2;
                break;

        }

    }
    /// <summary>
    /// 保存信息条
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSaveClass_Click(object sender, EventArgs e)
    {
        DictionaryService server = new DictionaryService();
        DictionaryInfo model = new DictionaryInfo();
        model.id = this.btnSaveClass.CommandArgument.ToInt(0);
        model.Nodeid = this.lbnDefaultAdd.CommandArgument.ToInt(0);
        model.Name = this.txtClassName.Text;
        model.Type = this.ddlType.SelectedValue;
        model.ParentID = 0;
        //如果存在封面
        if (fuImagePath.HasFile)
        {
            CoFileHepler.DeleteFile(this.Application["WebPath"] + this.labImagePath.Text);
            model.ImagePath = this.fuImagePath.FileName.GetFileExt().IsNullOrEmpty() ? "" : WebConfigInfo.ImgPath + Guid.NewGuid().ToString() + this.fuImagePath.FileName.GetFileExt();
            upFile(fuImagePath, model.ImagePath, this.Application["WebPath"].ToString(), true, WebConfigInfo.Imgformat);
        }
        else
        {
            model.ImagePath = labImagePath.Text;
        }
        //如果存在附件
        if (this.fuDictionaryFile.Visible && this.fuDictionaryFile.HasFile)
        {
            CoFileHepler.DeleteFile(this.Application["WebPath"] + this.txtValues.Text);
            model.Values = this.fuDictionaryFile.FileName.GetFileExt().IsNullOrEmpty() ? "" : WebConfigInfo.FilePath + Guid.NewGuid().ToString() + this.fuDictionaryFile.FileName.GetFileExt();
            upFile(fuDictionaryFile, model.Values, this.Application["WebPath"].ToString(), true, WebConfigInfo.Fileformat);
        }
        else
        { model.Values = this.txtValues.Text; }
        bool isAdd = false;
        if ("0".Equals(model.id.ToString()))
        {
            isAdd = server.Add(model);
        }
        else
        {
            isAdd = server.Update(model);
        }
        if (isAdd)
        {
            MessageBox.Show(this.Page, "操作成功！");
        }
        else
        {
            MessageBox.Show(this.Page, "操作失敗！");
        }
        this.MultiView1.ActiveViewIndex = 2;
        BindTable(this.lbnDefaultAdd.CommandArgument);
    }
    /// <summary>
    /// 首页模块切换
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbnDefaultAdd_Click(object sender, EventArgs e)
    {
        this.MultiView1.ActiveViewIndex = 2;
        BindTable(this.lbnDefaultAdd.CommandArgument);

    }

    /// <summary>
    /// 选择信息类别
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
    {

        seleteTag();
    }
    /// <summary>
    /// 变化类型
    /// </summary>
    private void seleteTag()
    {
        switch (this.ddlType.SelectedValue)
        {
            case "http":
                this.txtValues.Visible = true;
                this.fuDictionaryFile.Visible = false;
                break;
            case "file":
                this.fuDictionaryFile.Visible = true;
                this.txtValues.Visible = false;
                break;
            default:
                this.txtValues.Visible = true;
                this.fuDictionaryFile.Visible = false;
                break;
        }
    }

    /// <summary>
    /// 上傳操作
    /// </summary>
    /// <param name="Fupload">上傳空間</param>
    /// <param name="SaveName">保存名</param>
    /// <param name="dir">保存目錄</param>
    /// <param name="CheckType">驗證類型字符串</param>
    /// <returns></returns>
    private bool upFile(System.Web.UI.WebControls.FileUpload Fupload, string SaveName, string dir, bool isNULL, string CheckType)
    {
        //判断上传文件是否是空
        if (!Fupload.HasFile && isNULL)
        {
            return true;
        }
        //判断目录是否存在不存在则创建
        if (!Directory.Exists(dir))
        {
            Directory.CreateDirectory(dir);
        }
        //删除久的文件
        if (new FileInfo(dir + SaveName).Exists)
        {
            CoFileHepler.DeleteFile(dir + SaveName);
        }
        try
        {
            //獲得文件類型
            string type = CoFileHepler.GetPostfixStr(Fupload.FileName);
            //判斷文件類型是否為需要的類型
            if (CoFileHepler.CheckValidExt(type, CheckType))
            {
                Fupload.PostedFile.SaveAs(dir + SaveName);
            }
            return true;
        }
        catch
        {
            return false;
        }
    }
    /// <summary>
    /// 进入信息添加页面
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAddDictionary_Click(object sender, EventArgs e)
    {
        this.MultiView1.ActiveViewIndex = 3;
        this.btnSaveClass.CommandArgument = "0";
    }
    #endregion


}
